package com.jx.cashier;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.fasterxml.jackson.databind.JsonNode;
import com.jx.cashier.card.CardCallback;
import com.jx.cashier.card.ReaderCard;
import com.jx.cashier.card.ReaderCardFactory;
import com.jx.cashier.rest.RestApiMgr;
import com.jx.cashier.rest.RestApiMgrImpl;
import com.jx.cashier.rest.http.JxDccRestResponse;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private Button readCardButton;
    private ReaderCard reader;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private RestApiMgr restApiMgr = new RestApiMgrImpl(handler);

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        readCardButton = (Button) findViewById(R.id.button);
        readCardButton.setOnClickListener(uiOnClickListener);

        reader = ReaderCardFactory.createReader(this);

        reader.setReadCallback(new CardCallback() {
            @Override
            public void refreshCardNo(String cardNo) {
                mTextMessage.setText("卡号：" + cardNo);
            }
        });


    }

    View.OnClickListener uiOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            reader.startRead();

            restApiMgr.login("admin", "pass");
        }
    };

}
